import grpc_tools.protoc


def compile_proto(proto_file):
    arguments = [
        f'--proto_path={proto_file.rsplit("/", 1)[0] if "/" in proto_file else "."}',  # 根据需要调整路径
        '--python_out=.',
        '--grpc_python_out=.',
        proto_file
    ]

    if grpc_tools.protoc.main(arguments) != 0:
        raise Exception(f'Failed to compile {proto_file}')


if __name__ == '__main__':
    compile_proto('data.proto')  # 替换为你的.proto文件路径